<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript">
    $(document).ready(function() {
        Page.render('#fwTabs', '#fwTabs_template', TAB.fw);
        Page.render('#url_default_box', '#urldefaultBox_template', DOC);
        Page.render('#fwBtns', '#fwBtns_template', DOC);
        Page.render('#default_box', '#defaultBox_template', DOC);

        var $detailBox = $('.detail_box').hide();
        var $lis = $("ul.tabs a");
        var $boxes = $("div.tab_boxes > div"), $btnAdd = $('#btnAdd'), $btnApply = $('#btnApply');// $btnCleanAll = $('#btnCleanAll');
        var $btnSaveDefault = $('#btnSaveDefault');
        var $btnCleanRules = $('#btnCleanRules');
        //var $btnApplyAll = $('#btnApplyAll');
        var $box = $('#edit_box'), $mask = $('#mask');
        var $acceptAlls = $('input[name=acceptAll]'),
            $acceptAll6s = $('input[name=acceptAll6]');
        var $btnShifts = $('input[id^=btnShift]'), $lblShift = $('#lblShift');
        var currentPannelIndex = 0;
        var isPageRequests = [false, false, false, false, false, false, false, false];
        var defaultCmd = RequestCmd.DEFAULT_FILTER;
        var Action_Event = {Default: -1, Add_New_Item: 1, Set_Shift_Stat: 2, Edit_Item: 3};
        $lis.click(function() {
            $lis.removeClass("current");
            $(this).addClass("current");
            var pannelIndex = $lis.index(this);
            $boxes.eq(pannelIndex).show().siblings().hide();

            var hasEnableLink = true;
            currentPannelIndex = pannelIndex;
            if (pannelIndex == 8) {
                getDefaultSettingsDatas();
                $lblShift.hide();
                $btnShifts.hide();
                $btnAdd.hide();
                $btnApply.hide();
                $btnCleanRules.hide();
            }else if(pannelIndex == 9) {
                getUrlDefault();
                $lblShift.hide();
                $btnShifts.hide();
                $btnAdd.hide();
                $btnApply.hide();
                $btnCleanRules.hide();
            }else {
                getDefaultSettingsDatas()
                $lblShift.show();
                $btnShifts.show();
                $btnAdd.show();
                $btnApply.show();
                $btnCleanRules.show();
            }

            function getDefaultSettingsDatas() {
                    var cmd =  filterOptions.getRequestCmd(pannelIndex);
                    Page.postJSON({
                        json: {
                            cmd: cmd,
                            method: JSONMethod.GET,
                            getfun: true
                        },
                        success: function(data) {
                            if (pannelIndex == 8) {
                                if (data.action) {
                                    $acceptAlls.each(function(){
                                        if($(this).val() == data.action.toString()){
                                            $(this).attr("checked", "checked");
                                        }
                                    });
                                }
                            } else {
                                createFilterTable({
                                    pannelIndex: pannelIndex,
                                    datas: data.data || [],
                                    requiredSave: false,
                                    hasEnableLink: hasEnableLink,
                                    customTitles: customTitles,
                                    customFieldNames: customFieldNames
                                });
                            }
                        }
                    });
                };

            if (!isPageRequests[pannelIndex]) {
                // isPageRequests[pannelIndex] = true;

                var customTitles = [], customFieldNames = [];
                switch (pannelIndex) {
                    case 0:
                        customTitles = [DOC.net.protocol, DOC.net.port, DOC.lbl.remark];
                        customFieldNames = [ "protocol", "port", "comment"];
                        break;
                    case 1:
                        customTitles = [ DOC.net.protocol, DOC.net.targeAddr, DOC.lbl.remark];
                        customFieldNames = ["protocol", "ipaddr", "comment"];
                        break;
                    case 2:
                        customTitles = [ DOC.net.mac, DOC.lbl.remark];
                        customFieldNames = ["mac", "comment"];
                        break;
                    case 3:
                        hasEnableLink = false;
                        customTitles = [DOC.net.portStart,DOC.net.portEnd,DOC.net.mappingIp, DOC.net.mappingPort, DOC.lbl.remark];
                        customFieldNames = ["dest_port","dest_port_end", "redirect_addr", "redirect_port", "comment"]
                        break;
                    case 4:
                        customTitles = [DOC.net.url,DOC.lbl.advancedOptions, DOC.lbl.remark];
                        customFieldNames = ["url","interface", "comment"];
                        break;
                    case 5:
                        hasEnableLink = false;
                        customTitles = [DOC.net.targeAddr, DOC.net.mac, DOC.lbl.remark];
                        customFieldNames = ["ipaddr", "mac", "comment"];
                        break;
                    case 6:
                        hasEnableLink = false;
                        customTitles = [ DOC.net.targeAddr, DOC.net.maxSpeed, DOC.lbl.remark];
                        customFieldNames = [ "ipaddr", "speed", "comment"];
                        break;
                    case 7:
                        customTitles = [DOC.net.protocol,DOC.net.sourceAddr,DOC.net.targeAddr, DOC.lbl.remark];
                        customFieldNames = [ "protocol","src_ipaddr","dest_ipaddr", "comment"];
                        break;
                    default:
                        break;
                }                       
            } else {
                createTableDelegate(Action_Event.Set_Shift_Stat);
            }
        });
    var flags = false;
        $btnShifts.click(function() {
            flags = true;
            createTableDelegate($btnShifts.index(this));
        });

        $acceptAll6s.click(function() {
            Page.postJSON({
                json: {
                    cmd: defaultCmd,
                    method: JSONMethod.POST,
                    success: true,
                    datas: [{enableRule: true, acceptAll: eval($('input[name=acceptAll]').filter(':checked').val()), ippro: "IPV4"},
                        {enableRule: true, acceptAll: eval($('input[name=acceptAll6]').filter(':checked').val()), ippro: "IPV6"}]
                },
                success: function(data) {
                    $btnApply.enable();
                }
            });
        });


        function getUrlDefault(){
            Page.postJSON({
                json: {
                    cmd: RequestCmd.FIREWALL_URL_GET,
                    method: JSONMethod.GET
                },
                success:function(data){
                    if(data.data == "ACCEPT"){
                        $("input[value='ACCEPT']").prop("checked",true);
                    }else if(data.data == "DROP"){
                        $("input[value='DROP']").prop("checked",true);
                    }
                }

            })


        }

        $("#btnSaveDefault2").on("click",function(){
            $(this).disable();
            var json = new Object();
            json.cmd = RequestCmd.FIREWALL_URL_SET;
            json.method = JSONMethod.POST;
            json.url_default = $("input[name='acceptAll2']:checked").val();
            Page.postJSON({
                json:json,
                success:function(data){
                    if(data.success == true){
                        AlertUtil.alertMsg(PROMPT.status.applyRuleSuccess);
                    }
                },
                complete:function(){
                    $("#btnSaveDefault2").enable();
                }

            })
        });

        function showEditBox(data)
        {
            $mask.show();
            $box.addClass('edit_box').show(); 
            SysUtil.setBoxStyle($box);

            switch(currentPannelIndex) {
                case 0:
                    processPortFilter(data);
                    $box.addClass('edit_box').show(); 
                    break;

                case 1:
                    processIpFilter(data);
                    $box.addClass('edit_box').show(); 
                    break;

                case 2:
                    processMacFilter(data);
                    $box.addClass('edit_box').show(); 
                    break;

                case 3:
                    processPortMapping(data);
                    $box.addClass('edit_box').show(); 
                    break;

                case 4:
                    processUrlFilter(data);
                    $box.addClass('edit_box').show(); 
                    break;

                case 5:
                    processIpMacBinding(data);
                    $box.addClass('edit_box').show(); 
                    break;

                case 6:
                    processSpeedLimit(data);
                    $box.addClass('edit_box').show(); 
                    break;

                case 7:
                    processAclFilter(data);
                    $box.addClass('edit_box').show(); 
                    break;

                default:
                    break;
            }
        }

        $btnAdd.click(function() {
            showEditBox();
        });


        $btnApply.click(function() {
            // AlertUtil.alertMsg(PROMPT.status.applyRuleSuccess);
            Page.postJSON({
                //$id: $btnApply,
                json: {
                    cmd: RequestCmd.APPLY_ALL_RULES,
                    method: JSONMethod.POST,
                    // action: $('input[name="acceptAll"]:checked').val()
                },
                success: function(data) {
                    if (data.success == "success") {
                        AlertUtil.alertMsg(PROMPT.status.applyRuleSuccess);
                    } else {
                        AlertUtil.alertMsg(PROMPT.status.applyRuleFail);
                    }
                }
            });
        });

       /* $btnApplyAll.click(function() {
            // AlertUtil.alertMsg(PROMPT.status.applyRuleSuccess);
            Page.postJSON({
                //$id: $btnApply,
                json: {
                    cmd: RequestCmd.APPLY_FILTER,
                    method: JSONMethod.POST
                },
                success: function(data) {
                    if (data.success == "success") {
                        AlertUtil.alertMsg(PROMPT.status.applyRuleSuccess);
                    } else {
                        AlertUtil.alertMsg(PROMPT.status.applyRuleFail);
                    }
                }
            });
        });*/

        $btnSaveDefault.click(function() {
            $btnSaveDefault.attr("disabled", true);
            var json = {};
            json.cmd = RequestCmd.APPLY_FILTER;
            json.method = JSONMethod.POST;
            json.action = $('input[name="acceptAll"]:checked').val();
            Page.postJSON({
                json: json,
                success: function(data) {
                    if (data.success == "success") {
                        AlertUtil.alertMsg(PROMPT.saving.success);
                    } else {
                        AlertUtil.alertMsg(PROMPT.saving.fail);
                    }
                    $btnSaveDefault.attr("disabled", false);
                }
            });
        });

      /*  $btnCleanAll.click(function() {
            if(!confirm(PROMPT.confirm.clearAllRules)){
                return false;
            }

            Page.postJSON({
                json: {
                    cmd: RequestCmd.CLEAN_ALL_FILTER,
                    method: JSONMethod.POST
                },
                success: function(data) {
                    if (data.success) {
                        AlertUtil.alertMsg(PROMPT.status.clearAllRulesSuccess);
                    } else {
                        AlertUtil.alertMsg(PROMPT.status.clearAllRulesFail);
                    }
                },
                complete: function() {

                    $('#FW_RULE').click();

                }
            });
        });*/

        $btnCleanRules.click(function() {
            if(!confirm(PROMPT.confirm.clearRules)){
                return false;
            }

            var clear_param =  [
                'portFilter',
                'ipFilter',
                'macFilter',
                'portMapping',
                'urlFilter',
                'ipMacBinding',
                'speedLimit',
                'aclFilter',
                ''
            ]

            Page.postJSON({
                json: {
                    cmd: RequestCmd.CLEAN_ALL_FILTER,
                    datas:{param:clear_param[currentPannelIndex]},
                    method: JSONMethod.POST
                },
                success: function(data) {
                    if (data.success) {
                        AlertUtil.alertMsg(PROMPT.status.clearRulesSuccess);
                    } else {
                        AlertUtil.alertMsg(PROMPT.status.clearRulesFail);
                    }
                },
                complete: function() {
                    //$lis.eq(8).click();
                    //$('#FW_RULE').click();
                    $lis.each(function(){
                        if($lis.index(this) == currentPannelIndex )
                        {
                            isPageRequests[currentPannelIndex] = false;
                            $(this).click();
                        }
                    });
                }
            });
        });
        function createTableDelegate(shiftIndex) {
            $('#hddShiftIndex' + currentPannelIndex).val(shiftIndex);
            $('#btnDelegate' + currentPannelIndex).click();
        }

        function shiftItem(shiftIndex, selectedIndex, length) {
            switch(shiftIndex) {
                case 0: selectedIndex = 0; break;
                case 1: selectedIndex--; break;
                case 2: selectedIndex++; break;
                case 3: selectedIndex = length; break;
                default: break;
            }
            return selectedIndex;
        }

        var filterOptions = {
            requiredSave: true,
            hasEnableLink: true,
            pannelIndex: 0,
            selectedIndex: 0,
            listBoxIds: ['port_box', 'ip_box', 'mac_box', 'port_mapping_box', 'url_box', 'ip_mac_binding_box', 'speed_limit_box', 'acl_box', 'default_box',"url_default_box"],
            editBoxId: 'edit_box',
            btnSaveId: 'btnSaveRule',
			lanArr:[],
            datas: [],
            getDatas: function() {
                return this.datas;
            },
            setDatas: function(datas) {
                this.datas = datas;
            },
            urls: [
                'html/firewall/portFilter.html',
                'html/firewall/ipFilter.html',
                'html/firewall/macFilter.html',
                'html/firewall/portMapping.html',
                'html/firewall/urlFilter.html',
                'html/firewall/ipMacBinding.html',
                'html/firewall/speedLimit.html',
                'html/firewall/aclFilter.html',
                '',
                ''

            ],
            cmds: [
                RequestCmd.PORT_FILTER,
                RequestCmd.IP_FILTER,
                RequestCmd.MAC_FILTER,
                RequestCmd.OTHER_FILTER,
                RequestCmd.URL_FILTER,
                RequestCmd.IP_MAC_BINDING,
                RequestCmd.SPEED_LIMIT,
                RequestCmd.ACL_FILTER,
                RequestCmd.DEFAULT_FILTER

            ],
            customTitles: [],
            customFieldNames: [],
            getRequestCmd: function(pannelIndex) {
                return this.cmds[pannelIndex || this.pannelIndex];
            },
            getUrl: function() {
                return this.urls[this.pannelIndex];
            },
            getListBoxId: function() {
                return this.listBoxIds[this.pannelIndex];
            },
            getIdByIndex: function(i) {
                return (this.pannelIndex + 1) * 10000 + i;
            },
            getIndexById: function(id) {
                var num = parseInt(id, 10);
                if (!isNaN(num)) {
                    return num % 10000;
                }
                return 0;
            },
            setApplyButtonStatus: function(value) {
                if (!value) {
                    $btnApply.disable();
                } else {
                    $btnApply.enable();
                }
            },
            setShiftButtonsStatus: function() {
                var selectedIndex = this.getSelectedIndex();
                var length = this.getDatas().length;
                if (length <= 1) {
                    $btnShifts.disable();
                } else {
                    if (selectedIndex == 0) {
                        $btnShifts.eq(0).disable();
                        $btnShifts.eq(1).disable();
                        $btnShifts.eq(2).enable();
                        $btnShifts.eq(3).enable();
                    } else if (selectedIndex == (length - 1)) {
                        $btnShifts.eq(0).enable();
                        $btnShifts.eq(1).enable();
                        $btnShifts.eq(2).disable();
                        $btnShifts.eq(3).disable();
                    } else {
                        $btnShifts.enable();
                    }
                }
            },
            setSelectedIndex: function(selectedIndex) {
                this.selectedIndex = selectedIndex;
            },
            getSelectedIndex: function() {
                return this.selectedIndex;
            },
            getItem: function() {
                return JSON.parse($('#hddItem' + this.pannelIndex).val());
            },
            setItem: function(item) {
                $('#hddItem' + this.pannelIndex).val(JSON.stringify(item));
            },
			getLanArr: function () {
                return $('#hddLAN').val();
            },
            setLanArr: function (arr) {
				 $('#hddLAN').val(arr);
            },
            setRowDataInBox: function(data) {
                if(data == undefined){
                    return;
                }else {
                    for(var name in data)
                    {
                        if($(String.format("input[name={0}]", name)).attr("type") == "radio"){
                            $(String.format("input[name={0}][value={1}]", name, data[name])).attr("checked",true);
                        }else {
                            $(String.format("input[name={0}]", name)).val(data[name]);
                        }
                    }
                }
            }
        };

        function createFilterTable(options) {
            options = $.extend({}, filterOptions, options || {});
            var datas = options.getDatas();
            if (options.requiredSave) {
                Page.postJSON({
                    json: {
                        cmd: options.getRequestCmd(),
                        method: JSONMethod.POST,
                        success: true,
                        datas: datas
                    },
                    success: function(data) {
                    }
                });
            } else {
                options.requiredSave = true;
            }
            
            var sb = new StringBuilder();
            sb.append('<table class="list" cellspacing="0">');
            sb.append(String.format('<tr><th>{0}</th><th>{1}</th><th class="index">{2}</th>',
                DOC.table.enableRule, DOC.table.selected, DOC.table.priority));

            var titles = options.customTitles;
            var pannelIndex = options.pannelIndex;
            for (var i = 0; i < titles.length; i++) {
                if(titles[i] != "IP")
                    sb.append(String.format('<th>{0}</th>', titles[i]));
            }

            var hasEnableLink = options.hasEnableLink;
            if (hasEnableLink) {
                sb.append(String.format('<th>{0}</th><th>{1}</th>',
                    DOC.table.filterMode, DOC.table.changeFilterMode));
            }
            sb.append(String.format('<th>{0}</th><th>{1}', DOC.table.editRule,DOC.table.delRule));
			if(pannelIndex == 4){
                sb.append('<input type="hidden" id="hddLAN" value="null" />');
            }
            sb.append(String.format('<input type="hidden" id="hddItem{0}" value="null" />', pannelIndex));

            sb.append(String.format('<input type="hidden" id="hddShiftIndex{0}" value="0" />', pannelIndex));
            sb.append(String.format('<span style="display:none"><input type="button" id="btnDelegate{0}" value="Delegate" /></span>', pannelIndex));
            sb.append('</th></tr>');
            var length = datas.length;
            var selectedIndex = options.getSelectedIndex();
            if (selectedIndex >= length) {
                selectedIndex = length - 1;
            }
            if (selectedIndex < 0) {
                selectedIndex = 0;
            }
            // 保存修改后的级别
            options.setSelectedIndex(selectedIndex);
            options.setShiftButtonsStatus();

            var fieldNames = options.customFieldNames;
            for(var i = 0; i < length; i++) {

                var item = datas[i];
                if (item == null) continue;

                var id = options.getIdByIndex(i);
                sb.append(String.format('<tr id="trRule{0}"{1}>', id, !item.iswork ? ' class="disabled"' : ''));
                sb.append(String.format('<td><input type="checkbox" id="chkRule{0}" {1} /></td>', id,
                    item.iswork ? ' checked="checked"' : ''));
                sb.append(String.format('<td><input type="radio" id="radRule{0}" name="radRule{1}" {2} /></td>', id, pannelIndex,
                    selectedIndex == i ? ' checked="checked"' : ''));
                sb.append(String.format('<td>{0}</td>', i + 1));

                for (var j = 0; j < fieldNames.length; j++) {
                    if(fieldNames[j] != "dest_addr")
                        sb.append(String.format('<td>{0}</td>', eval("item." + fieldNames[j]) || ""));
                }

                if (hasEnableLink) {
                    sb.append(String.format('<td>{0}</td>', item.action == "ACCEPT" ?
                        String.format('<span class="ok">{0}</span>', DOC.status.accept) :
                        String.format('<span class="fail">{0}</span>', DOC.status.reject)));
                    sb.append(String.format('<td><a href="javascript:;" id="lnkEnableLink{0}" class="common">{1}</a></td>', id,
                        item.action == "ACCEPT"? DOC.status.reject : DOC.status.accept));
                }
				sb.append(String.format('<td><a href="javascript:;" id="lnkRuleEdit{0}">{1}</a></td>', id, DOC.table.edit));
                sb.append(String.format('<td><a href="javascript:;" id="lnkRuleDel{0}">{1}</a></td>', id, DOC.table.del));
               

                sb.append('</tr>');
            }
            sb.append('</table>');
            $boxes.eq(pannelIndex).html(sb.toString());
            Page.setStripeTable(String.format('#{0} table', options.getListBoxId()));

            $('#btnDelegate' + pannelIndex).click(function() {

                var shiftIndex = parseInt($('#hddShiftIndex' + pannelIndex).val(), 10);
                if(flags ==  true){
                    var selectedIndex = options.getSelectedIndex();
                    var item = datas[selectedIndex];
                    datas.splice(selectedIndex, 1);

                    selectedIndex = shiftItem(shiftIndex, selectedIndex, datas.length);
                    datas.splice(selectedIndex, 0, item);

                    options.setApplyButtonStatus(true);
                    options.setSelectedIndex(selectedIndex);
                }else{
                if (shiftIndex  == Action_Event.Add_New_Item) {
                    datas.push(options.getItem());
                    options.setApplyButtonStatus(true);
                    options.setSelectedIndex(datas.length - 1);
                } else if (shiftIndex == Action_Event.Set_Shift_Stat) {
                    options.setShiftButtonsStatus();
                } else if (shiftIndex == Action_Event.Edit_Item) {
                    var selectedIndex = options.getSelectedIndex();
                    datas[selectedIndex] = $.extend({}, datas[selectedIndex], options.getItem());
                    options.setApplyButtonStatus(true);
                } else {
                    var selectedIndex = options.getSelectedIndex();
                    var item = datas[selectedIndex];
                    datas.splice(selectedIndex, 1);

                    selectedIndex = shiftItem(shiftIndex, selectedIndex, datas.length);
                    datas.splice(selectedIndex, 0, item);

                    options.setApplyButtonStatus(true);
                    options.setSelectedIndex(selectedIndex);
                 }
                }
                createFilterTable(options);
            });

            $(String.format('tr[id^=trRule{0}]', pannelIndex + 1)).click(function() {
                $(this.id.replace("trRule", "#radRule")).click();
            });

            $(String.format('input[name=radRule{0}]', pannelIndex)).click(function(e) {
                // 阻止事件冒泡
                e.stopPropagation();

                var selectedIndex = options.getIndexById(this.id.replace("radRule", ""));

                options.setSelectedIndex(selectedIndex);
                options.setShiftButtonsStatus();
            });

            $(String.format('input[id^=chkRule{0}]', pannelIndex + 1)).click(function(e) {
                e.stopPropagation();

                var selectedIndex = options.getIndexById(this.id.replace("chkRule", ""));
                try {
                    var item = datas[selectedIndex];
                    if ($(this).attr("checked")) {
                        item.iswork = true;
                    } else {
                        item.iswork = false;
                    }

                    options.setApplyButtonStatus(true);
                    createFilterTable(options);
                } catch(e) {
                }
            });

            $(String.format('a[id^=lnkEnableLink{0}]', pannelIndex + 1)).click(function(e) {
                e.stopPropagation();
                var selectedIndex = options.getIndexById(this.id.replace("lnkEnableLink", ""));
                try {
                    var item = datas[selectedIndex];
                    if (item.iswork) {
                        item.action = item.action == "ACCEPT" ? "DROP" : "ACCEPT";
                        options.setApplyButtonStatus(true);
                        createFilterTable(options);
                    }
                } catch(e) {
                }
            });

            $(String.format('a[id^=lnkRuleEdit{0}]', pannelIndex + 1)).click(function(e) {
                e.stopPropagation();
                var fullIndex = this.id.replace("lnkRuleEdit", "");
                var selectedIndex = options.getIndexById(fullIndex);
                try {
                    $(String.format('input[id^=radRule{0}]', fullIndex)).attr("checked", true);
                    options.setSelectedIndex(selectedIndex);
                    options.setShiftButtonsStatus();
                    showEditBox(datas[selectedIndex]);
                } catch(e) {
                }
            });

            $(String.format('a[id^=lnkRuleDel{0}]', pannelIndex + 1)).click(function(e) {
                e.stopPropagation();

                var selectedIndex = options.getIndexById(this.id.replace("lnkRuleDel", ""));
                try {
                    datas.splice(selectedIndex, 1);

                    options.setApplyButtonStatus(true);
                    createFilterTable(options);
                } catch(e) {
                }
            });
        }

        function processFilter(options) {
            options = $.extend({}, filterOptions, options || {});
            options.pannelIndex = currentPannelIndex;

            // Page.getHtml(options.getUrl(), MenuItem.FW_RULE.cmd + options.pannelIndex, function(data) {
            $box.load(options.getUrl(),function () {


                // $box.html(data);
                $box.css({
                    width: options.width,
                    height: options.height
                });
				var title = $('#edit_title').text();
                if(options.rowData == undefined) {
                    $('#edit_title').text(DOC.table.add + title);
                }
                else{
                    $('#edit_title').text(DOC.table.edit + title);
                }


                var tab = String.format('#{0} table', options.editBoxId);
                Page.setStripeTable(tab);
                $(tab + ' input[type=text]:first').focus();
                if(options.pannelIndex == 4){
                    var LanArr = options.getLanArr();
                    if(LanArr != "null"){
						var lan_arr = LanArr.split(",");
                        $.each(lan_arr,function(index,value){
                            $($('input[name=interface]')[index]).attr('value',value);	
							$($('span[name=faceSpan]')[index]).html(value);						
                        });
						if(options.rowData != undefined) {
							if(options.rowData["interface"] != undefined && options.rowData["interface"] != "" ){
                                $("#lanOptions").click();
                                $('#lanDiv').show();
                                $(String.format("input[name=interface][value={0}]", options.rowData["interface"])).attr("checked",true);
                            }
						}
 
                    }else {
                        Page.postJSON({
                            json: {
                                cmd: RequestCmd.LAN_INFO,
                                method: JSONMethod.GET
                            },
                            success: function(data) {
                                var lan_arr = data.data;
                                options.setLanArr(lan_arr);
                                $.each(lan_arr,function(index,value){
								   $($('input[name=interface]')[index]).attr('value',value);
								   $($('span[name=faceSpan]')[index]).html(value);
                                });
                                if(options.rowData != undefined) {
								  if(options.rowData["interface"] != undefined && options.rowData["interface"] != "" ){
									$("#lanOptions").click();
									$('#lanDiv').show();
									$(String.format("input[name=interface][value={0}]", options.rowData["interface"])).attr("checked",true);
								   }
								}
                            }
                        });
                    }

                }
                options.setRowDataInBox(options.rowData);

                $('#' + options.btnSaveId).click(function() {
				   flags = false;
                    var item = options.getSavingItem();
                    if (!item) return;

                    options.setItem(item);
                    if(options.rowData == undefined){
					 createTableDelegate(Action_Event.Add_New_Item);
					}else{
					 createTableDelegate(Action_Event.Edit_Item);
					}

                    $mask.hide();
                    $box.removeClass('edit_box').hide();
                });
            });
            // });
        }

        function getCommonItem(hasIPV46) {
            var item = {};

            //item.enableRule = true;
            item.protocol = $('input[name=protocol]').filter(':checked').val();

            var $enableLinks = $('input[name=action]');
            if ($enableLinks.length > 0) {
                item.action = $enableLinks.filter(':checked').val();
            }
            //item.remark = $('#txtRemark').val().trim();
            item.comment = $('#comment').val().trim();
            if(hasIPV46){
                item.ippro = $('input[name=ippro]').filter(':checked').val();
            }

            return item;
        }

        function processSpeedLimit(data) {
            processFilter({
                width: "700px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                    var ip = $('#ipaddr').val().trim().replace(/ /g, '');
                    if (ip == "") {
                        AlertUtil.alertMsg(CHECK.required.ip);
                        return false;
                    }

                    var item = getCommonItem(false);
                    if (!CheckUtil.checkIp(ip, item.ippro)) {
                        AlertUtil.alertMsg(CHECK.format.ip);
                        return false;
                    }

                    var maxSpeed = $('#speed').val().trim();
                    if (maxSpeed == "") {
                        AlertUtil.alertMsg(CHECK.required.maxSpeed);
                        return false;
                    }

                    var theMaxSpeed = parseInt(maxSpeed, 10);
                    if (isNaN(theMaxSpeed) || theMaxSpeed <= 0) {
                        AlertUtil.alertMsg(CHECK.format.maxSpeed);
                        return false;
                    }

                    item.ipaddr = ip;
                    item.speed = theMaxSpeed ;
                    if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });
        }

        function processIpMacBinding(data) {
            processFilter({
                width: "700px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                    var ip = $('#ipaddr').val().trim().replace(/ /g, '');
                    if (ip == "") {
                        AlertUtil.alertMsg(CHECK.required.ip);
                        return false;
                    }

                    var item = getCommonItem(false);
                    if (!CheckUtil.checkIp(ip, item.ippro)) {
                        AlertUtil.alertMsg(CHECK.format.ip);
                        return false;
                    }

                    var mac = $('#mac').val().trim().toUpperCase().replace(/ /g, '').replace(/\-/g, ':');
                    if (mac == "") {
                        AlertUtil.alertMsg(CHECK.required.mac);
                        return false;
                    }

                    if (!CheckUtil.checkMac(mac)) {
                        AlertUtil.alertMsg(CHECK.format.mac);
                        return false;
                    }

                    item.ipaddr = ip;
                    item.mac = mac;
                    if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });
        }

        function processPortMapping(data) {
            processFilter({
                width: "800px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                 var destport = parseInt($('#dest_port').val(),10);
                      var portend = parseInt($('#dest_port_end').val(),10);
                        if (isNaN(destport)) {
                            AlertUtil.alertMsg(CHECK.required.port);
                            return false;
                        }

                        if (destport < 0 || destport > 65535 ) {
                            AlertUtil.alertMsg(CHECK.range.port);
                            return false;
                        }

                        if(isNaN(portend)){
                            portend = destport;
                        }else {
                            if (portend < 0 || portend > 65535) {
                                AlertUtil.alertMsg(CHECK.range.port);
                                return false;
                            }

                            if (portend < destport) {
                                AlertUtil.alertMsg(CHECK.format.portRange);
                                return false;
                            }
                        }

                    var mappingIp = $('#redirect_addr').val();
                    if (mappingIp == "") {
                        AlertUtil.alertMsg(CHECK.required.ip);
                        return false;
                    }

                    if (!CheckUtil.checkIp(mappingIp)){
                        AlertUtil.alertMsg(CHECK.format.ip);
                        return false;
                    }

                    var theMappingPort = $('#redirect_port').val();
                    if (theMappingPort == "") {
                        AlertUtil.alertMsg(CHECK.required.port);
                        return false;
                    }

                    var mappingPortArr = CheckUtil.checkPort(theMappingPort);
                    if (!mappingPortArr.isValid) {
                        AlertUtil.alertMsg(CHECK.format.port);
                        return false;
                    }
                    var mappingPort = mappingPortArr.port;

                    var item = getCommonItem();
                    item.dest_addr = location.hostname;
                    item.dest_port = destport;
                    item.dest_port_end = portend;
                    item.redirect_addr = mappingIp;
                    item.redirect_port = mappingPort;
                    if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });
        }

        function processUrlFilter(data) {
            processFilter({
                width: "800px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                    var url = $('#url').val().trim().replace(/ /g, '');
                    if (url == "") {
                        AlertUtil.alertMsg(CHECK.required.urlKeyword);
                        return false;
                    }

					if( $("#lanOptions").prop("checked")){
                        var interface = $('input[name=interface]').filter(':checked').val();
                    }else {
                        var interface = ""
                    }
                    var item = getCommonItem(false);
                    item.url = url;
					item.interface = interface;
                   if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });
        }

        function processMacFilter(data) {
            processFilter({
                width: "700px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                    var mac = $('#mac').val().trim().toUpperCase().replace(/\u200e/g, '').replace(/\-/g, ':');
                    if (mac == "") {
                        AlertUtil.alertMsg(CHECK.required.mac);
                        return false;
                    }

                    if (!CheckUtil.checkMac(mac)) {
                        AlertUtil.alertMsg(CHECK.format.mac);
                        return false;
                    }
                    var item = getCommonItem(false);
                    item.mac = mac;
                    if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });
        }

        function processIpFilter(data) {
            processFilter({
                width: "800px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                    var ip = $('#ipaddr').val().trim().replace(/ /g, '');
                    if (ip == "") {
                        AlertUtil.alertMsg(CHECK.required.ip);
                        return false;
                    }

                    var item = getCommonItem(false);
                    var ips = ip.split('-');

                    if (ips.length > 2 || !CheckUtil.checkIp(ips[0], item.ippro) || (ips.length == 2 && !CheckUtil.checkIp(ips[1], item.ippro))

                    ) {
                        AlertUtil.alertMsg(CHECK.format.ip);
                        return false;
                    }

                    item.ipaddr = ip;
                    if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });
        }

        function processPortFilter(data) {
            processFilter({
                width: "800px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                    var port = parsePort($('#port').val());
                    if (!port) return false;

                    var item = getCommonItem(false);
                    item.port = port;
					if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });
        }

        function processAclFilter(data) {
            processFilter({
                width: "800px",
                height: "auto",
                rowData: data,
                getSavingItem: function() {
                    var srcip = $('#src_ipaddr').val().trim().replace(/ /g, '');
                    var destip = $('#dest_ipaddr').val().trim().replace(/ /g, '');
                    if (srcip == "" || destip == "") {
                        AlertUtil.alertMsg(CHECK.required.ip);
                        return false;
                    }

                    var item = getCommonItem(false);

                    var srcips = srcip.split('-');
                    var destips = destip.split('-');
                    if (srcips.length > 2 || !CheckUtil.checkIp(srcips[0], item.ippro) || (srcips.length == 2 && !CheckUtil.checkIp(srcips[1], item.ippro))

                    ) {
                        AlertUtil.alertMsg(CHECK.format.ip);
                        return false;
                    }

                    if (destips.length > 2 || !CheckUtil.checkIp(destips[0], item.ippro) || (destips.length == 2 && !CheckUtil.checkIp(destips[1], item.ippro))

                    ) {
                        AlertUtil.alertMsg(CHECK.format.ip);
                        return false;
                    }

                    item.src_ipaddr = srcip;
                    item.dest_ipaddr = destip;
                    if(data == undefined){
                        item.iswork = false;
                    }
                    return item;
                }
            });

        }

        function parsePort(thePort) {
            var port = thePort.trim().replace(/ /g, ''), port1, port2, pre = '';
            if (port == "") {
                AlertUtil.alertMsg(CHECK.required.port);
                return false;
            }
            if (port.startsWith('<') || port.startsWith('>')) {
                pre = port.substring(0, 1);
                port = port.substring(1);
            }

            var ports = port.split('-');
            if (ports.length <= 2) {
                port1 = parseInt(ports[0], 10);
                if (isNaN(port1)) {
                    AlertUtil.alertMsg(CHECK.format.port);
                    return false;
                } else if (port1 < 0 || port1 > 65535) {
                    AlertUtil.alertMsg(CHECK.range.port);
                    return false;
                } else if (ports.length == 2) {
                    port2 = parseInt(ports[1], 10);
                    if (pre != '' || isNaN(port2)) {
                        AlertUtil.alertMsg(CHECK.required.port);
                        return false;
                    }
                    if (port2 < 0 || port2 > 65535) {
                        AlertUtil.alertMsg(CHECK.range.port);
                        return false;
                    }
                    if (port2 < port1) {
                        AlertUtil.alertMsg(CHECK.format.portRange);
                        return false;
                    }
                    port = port1 + '-' + port2;
                } else {
                    port = pre + port1.toString();
                }

                return port;
            } else {
                AlertUtil.alertMsg(CHECK.required.port);
                return false;
            }
            return port;
        };

        function setLisState(arr) {
            $lis.hide();

            for (var i = 0, max = arr.length; i < max; i++) {
                // $lis.eq(arr[i]).show().click();
                $lis.eq(arr[i]).show();
            }
        }
        switch(Page.menuItem.cmd) {
            case RequestCmd.PORT_FILTER:
                setLisState([0, 1, 2, 8]);
                $lis.eq(0).click();
                break;

            case RequestCmd.OTHER_FILTER:
                setLisState([3]);
                $lis.eq(3).click();
                break;

            case RequestCmd.URL_FILTER:
                setLisState([4,9]);
                $lis.eq(4).click();
                break;

            case RequestCmd.IP_MAC_BINDING:
                setLisState([5]);
                $lis.eq(5).click();
                break;

            case RequestCmd.SPEED_LIMIT:
                setLisState([6]);
                $lis.eq(6).click();
                break;
				
            case RequestCmd.ACL_FILTER:
                setLisState([7]);
                $lis.eq(7).click();
                break;
        }
        $detailBox.show();
    });
</script>

<div class="detail_box">
    <div class="tab_menu">
        <ul class="tabs" id="fwTabs">
            <script type="text/template" id="fwTabs_template">
                <li><a href="javascript:;"><%- portFiltering %></a></li>
                <li><a href="javascript:;"><%- ipFiltering %></a></li>
                <li><a href="javascript:;"><%- macFiltering %></a></li>
                <li><a href="javascript:;"><%- portMapping %></a></li>
                <li><a href="javascript:;"><%- urlFiltering %></a></li>
                <li><a href="javascript:;"><%- ipMacBinding %></a></li>
                <li><a href="javascript:;"><%- speedLimit %></a></li>
                <li><a href="javascript:;"><%- alcFiltering %></a></li>
                <li><a href="javascript:;"><%- defaultSetting %></a></li>
                <li><a href="javascript:;"><%- urlDefaulte %></a></li>
            </script>
        </ul>
    </div>
    <div class="tab_menu_bottom"></div>

    <div class="tab_boxes">
        <div id="port_box"></div>
        <div id="ip_box"></div>
        <div id="mac_box"></div>
        <div id="port_mapping_box"></div>
        <div id="url_box"></div>
        <div id="ip_mac_binding_box"></div>
        <div id="speed_limit_box"></div>
        <div id="acl_box"></div>
        <div id="default_box" style="display: none;">
            <script type="text/template" id="defaultBox_template">
                <fieldset style="width: 60%;padding: 10px 40px;line-height: 25px;">
                    <legend><%- lbl.ipv4FilterRule %></legend>
                    <div>
                        <input type="radio" name="acceptAll" value="ACCEPT" />&nbsp;<%= lbl.accept %>
                    </div>
                    <div>
                        <input type="radio" name="acceptAll" value="DROP" />&nbsp;<%= lbl.reject %>
                    </div>
                    <br />
                    <div class="tab_btns" >
                        <input id="btnSaveDefault" type="button" value="<%- btn.save %>" />
                    </div>
                </fieldset>
                <fieldset style="width: 60%;padding: 10px 40px;line-height: 25px;display: none;">
                    <legend><%- lbl.ipv6FilterRule %></legend>
                    <div>
                        <input type="radio" name="acceptAll6" value="true" checked="checked" />&nbsp;<%= lbl.accept %>
                    </div>
                    <div>
                        <input type="radio" name="acceptAll6" value="false" />&nbsp;<%= lbl.reject %>
                    </div>
                </fieldset>
                <br />
                <br />
<!--
                <fieldset style="width: 60%;padding: 10px 40px;line-height: 25px;">
                <legend><%- lbl.ipAllRules %></legend>
                <br />
                <div class="tab_btns" >
                    <input id="btnApplyAll" type="button" value="<%- btn.applyRules %>" />
                    <input id="btnCleanAll" type="button" value="<%- btn.clearAllRules %>" />
                </div>
                </fieldset>
                <br />
                -->
            </script>
        </div>

       <div id="url_default_box">
             <script type="text/template" id="urldefaultBox_template">
                <fieldset style="width: 60%;padding: 10px 40px;line-height: 25px;">
                    <legend><%- lbl.ipv4FilterRule %></legend>
                    <div>
                        <input type="radio" name="acceptAll2" value="ACCEPT" checked="checked" />&nbsp;<%= lbl.accept %>
                    </div>
                    <div>
                        <input type="radio" name="acceptAll2" value="DROP" />&nbsp;<%= lbl.reject %>
                    </div>
                    <br />
                    <div class="tab_btns" >
                        <input id="btnSaveDefault2" type="button" value="<%- btn.save %>" />
                    </div>
                </fieldset>

            </script>
        </div>

    </div>
    <div class="tab_btns" id="fwBtns">
        <script type="text/template" id="fwBtns_template">
            <input id="btnAdd" type="button" value="<%- btn.addRule %>" />
            &nbsp;&nbsp;
            <label id="lblShift"><%- lbl.priority %></label>
            <input id="btnShiftTop" type="button" value="<%- btn.top %>" />
            <input id="btnShiftUp" type="button" value="<%- btn.moveUp %>" />
            <input id="btnShiftDown" type="button" value="<%- btn.moveDown %>" />
            <input id="btnShiftBottom" type="button" value="<%- btn.bottom %>" />
            &nbsp;&nbsp;
            <input id="btnApply" type="button" value="<%- btn.applyRules %>" />
            &nbsp;&nbsp;
            <input id="btnCleanRules" type="button" value="<%- btn.clearAllRules %>" />
        </script>
    </div>
</div>

